package com.hz.hongbao.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.awesome.android.sdk.publish.Splash;
import com.awesome.android.sdk.publish.enumbean.LayerErrorCode;
import com.awesome.android.sdk.publish.listener.IAwSplashListener;
import com.hz.hongbao.R;
import com.hz.hongbao.utils.PermissionHelper;


/**
 * <p>开屏窗口</p>
 * Edited by Alian Lee on 2016-01-13.
 */
public class SplashActivity extends Activity {

    private static final String TAG = "SplashActivity";
    private PermissionHelper mPermissionHelper;
    private Splash splash;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        // 移除标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_splash);

        // 当系统为6.0以上时，需要申请权限
        mPermissionHelper = new PermissionHelper(this);
        mPermissionHelper.setOnApplyPermissionListener(new PermissionHelper.OnApplyPermissionListener() {
            @Override
            public void onAfterApplyAllPermission() {
                Log.i(TAG, "All of requested permissions has been granted, so run app logic.");
                runApp();
            }
        });
        if (Build.VERSION.SDK_INT < 23) {
            // 如果系统版本低于23，直接跑应用的逻辑
            Log.d(TAG, "The api level of system is lower than 23, so run app logic directly.");
            runApp();
        } else {
            // 如果权限全部申请了，那就直接跑应用逻辑
            if (mPermissionHelper.isAllRequestedPermissionGranted()) {
                Log.d(TAG, "All of requested permissions has been granted, so run app logic directly.");
                runApp();
            } else {
                // 如果还有权限为申请，而且系统版本大于23，执行申请权限逻辑
                Log.i(TAG, "Some of requested permissions hasn't been granted, so apply permissions first.");
                mPermissionHelper.applyPermissions();
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        mPermissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mPermissionHelper.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 跑应用的逻辑
     */
    private void runApp() {
        //设置开屏
        setupSplashAd();
    }

    /**
     * 设置开屏广告
     */
    private void setupSplashAd() {
        // 创建开屏容器
        RelativeLayout splashLayout = (RelativeLayout) findViewById(R.id.rl_splash);

        splash = new Splash(SplashActivity.this, "20161015000234", "100502");
        //设置开屏容器
        splash.setSplashContainer(splashLayout);
        //开始请求广告
        splash.requestAwSplash();


        // 初始化广告监听对象
        splash.setSplashEventListener(new IAwSplashListener() {
            @Override
            public void onSplashPreparedFailed(LayerErrorCode errorCode) {
                Log.d("TAG","---------onSplashPreparedFailed--------->"+errorCode.getMsg());
                //失败时回调
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startActivity(new Intent(SplashActivity.this, MainActivity.class));
                        SplashActivity.this.finish();
                    }
                }, 4000);
            }
            @Override
            public void onSplashPrepared() {
                Log.d("TAG","-------onSplashPrepared----------->");
                //加载成功时回调
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startActivity(new Intent(SplashActivity.this, MainActivity.class));
                        SplashActivity.this.finish();
                    }
                }, 4000);
            }
            @Override
            public void onSplashExposure() {
                //展示成功时回调
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startActivity(new Intent(SplashActivity.this, MainActivity.class));
                        SplashActivity.this.finish();
                    }
                }, 4000);
            }
            @Override
            public void onSplashClosed() {
                //关闭广告时回调
            }
            @Override
            public void onSplashClicked() {
                //点击广告时回调
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (splash != null) {
            splash.onDestroy();
        }
        super.onDestroy();
    }

    //开屏页最好禁止用户对返回按钮的控制
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                || keyCode == KeyEvent.KEYCODE_HOME) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
